{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 文件：长期保存信息的一个信息数据集合\n",
    "- 常见操作\n",
    "\t- 打开关闭\n",
    "\t- 读取操作\n",
    "\t- 查找操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# open：打开一个文件\n",
    "- 参数如下\n",
    "- 路径\n",
    "- mode \n",
    "\t- r：只读   \n",
    "\t- w:只写，会覆盖  \n",
    "\t- a:追加    \n",
    "\t- r+ ：读写的方式打开，不存在会报错\n",
    "- f.read(n) : 读出n个字符，不写就是全部\n",
    "- f.readline() : 读出一行\n",
    "- list(f) : 读出全部\n",
    "- f.seek(偏移数，偏移点) ： 0是文件开头 1是当前位置 2是末尾    这里的偏移数的单位是字节\n",
    "- f.tell() : 当前文件位置，单位是字节\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['假若他日相逢\\n', '我将何以贺你\\n', '以沉默\\n', '以眼泪\\n']\n"
     ]
    }
   ],
   "source": [
    "# with 可以帮助我们在合适的时间关闭文件\n",
    "with open(r'D:\\python文件\\test.txt','r+') as f:  # r保证他不会转义\n",
    "    # f.write(\"假若他日相逢\\n我将何以贺你\\n以沉默\\n以眼泪\\n\")  \n",
    "    # x = f.readline()\n",
    "    # while x:\n",
    "    #     print(x)\n",
    "    #     print(f.tell())\n",
    "    #     x = f.readline()\n",
    "    print(list(f))  #  把内容+格式当成一个列表返回\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文件写入\n",
    "- f.write():写入内容，只能是字符串、\n",
    "- f.writelines() :用于可迭代的文件，如列表的写入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(r'D:\\python文件\\test.txt','w') as f:\n",
    "     f.write(\"假若他日相逢\\n我将何以贺你\\n以沉默\\n以眼泪\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(r'D:\\python文件\\test.txt','w') as f:\n",
    "    l = ['假若他日相逢\\n','我将何以贺你\\n','以沉默\\n','以眼泪\\n']\n",
    "    f.writelines(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 序列化(持久化)：把一些内容写入磁盘中 pickle.dump()\n",
    "# 反序列化： 把磁盘中的内容拿出来  pickle.load()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this is test\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "l = \"this is test\"\n",
    "with open(r'D:\\python文件\\test.txt','wb') as f :\n",
    "    pickle.dump(l,f)\n",
    "with open(r'D:\\python文件\\test.txt','rb') as f :\n",
    "    x = pickle.load(f)\n",
    "    print(x)\n",
    "    \n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# shelve(序列化工具)：相当于是一个数据库，使用键值对来存储数据\n",
    "- shelve,open打开一个数据库\n",
    "- shelve.close(),关闭\n",
    "- writeback = True 是强制写回的意思，用于修改不同步的问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'power', 'age': 18}\n"
     ]
    }
   ],
   "source": [
    "import shelve\n",
    "with shelve.open(r'D:\\python文件\\test.db',writeback=True) as she:\n",
    "    she['one'] = {\"name\":\"power\",\"age\":18}\n",
    "with shelve.open(r'D:\\python文件\\test.db') as she:\n",
    "    print(she['one'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
